package com.bjsxt.userscenter.springmvc.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bjsxt.userscenter.common.controller.BaseController;
import com.bjsxt.userscenter.common.util.ConstatFinalUtil;
import com.bjsxt.userscenter.common.util.PageInfoUtil;
import com.bjsxt.userscenter.users.pojo.AAdmins;
import com.bjsxt.userscenter.users.pojo.AAdminsEnum;
import com.bjsxt.userscenter.users.service.IUsersService;

/**
 * 不需要登陆就可以访问的Controller
 * @author WangshSxt
 */
@Controller
public class NoLoginController extends BaseController
{
	/**
	 * Service如何赋值
	 * 通过Dubbo赋值,
	 * Dubbo的消费者
	 */
	@Resource
	private IUsersService usersService;
	
	/**
	 * 第一个例子
	 * @return
	 */
	@RequestMapping("/hw")
	public String hw()
	{
		System.out.println("===第一个例子===");
		return "/head/hw";
	}
	
	/**
	 * Controller的方法和requestMapp和jsp的文件名三个一致
	 * 
	 * 字符串转换成日期的对象,放到Controller的方法中实现,
	 * 如果要用Spring自动赋值,自动转换;需要提供一个转换器
	 * @return
	 */
	@RequestMapping("/adminsList")
	public String adminsList(HttpServletRequest request , String currentPage,String pageSize,
			String keyword,String status,String st , String ed , 
			Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("==adminsList==");
		
		if("get".equalsIgnoreCase(request.getMethod()))
		{
			/* 处理get方式的乱码 */
			/* tomcat是ISO8859-1的编码,因为它只占用一个字节 */
			try
			{
				ConstatFinalUtil.SYS_LOGGER.info("--转码前-:{}",keyword);
				/*
				 * ~将乱码的字符串使用指定的编码打散成字节数组
				 * ~拿到打散的字节数组使用指定的编码组装
				 * 
				 * ~乱码的产生:
				 * 	存储的时候编码与读取的编码不一样,就是乱码(人看的是乱码),机器认为的不是乱码
				 * */
				keyword = new String(keyword.getBytes("ISO8859-1"),"UTF-8");
				ConstatFinalUtil.SYS_LOGGER.info("--转码后-:{}",keyword);
			} catch (Exception e)
			{
			}
		}
		
		/* 将字符串变成时间
		 * */
		if(st == null)
		{
			st = "" ; 
		}
		if(ed == null)
		{
			ed = "" ; 
		}
		if(status == null)
		{
			status = "" ; 
		}
		
		/* 搜索条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("keyword", keyword);
		if(!"".equalsIgnoreCase(status))
		{
			condMap.put("status", status);
		}
		
		Date stDate = null ; 
		Date edDate = null ;
		if(!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			/*将字符串转换成日期
			 * 
			 * 完善了DateFormatUtil
			 * */
			stDate = this.dateFormatUtil.strDateTime(st); 
			edDate = this.dateFormatUtil.strDateTime(ed); ; 
		}
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);
		
		PageInfoUtil pageInfoUtil = proccedPageInfo(currentPage, pageSize);
		
		/* 查询结果 */
		List<AAdmins> adminsList = this.usersService.findCondListAdminsService(pageInfoUtil,condMap);
		/* 将结果放到四大作用域中 */
		model.addAttribute("adminsList", adminsList);
		model.addAttribute("pageInfoUtil", pageInfoUtil);
		
		/* 将查询条件放到request中 */
		model.addAttribute("keyword", keyword);
		model.addAttribute("status", status);
		model.addAttribute("st", st);
		model.addAttribute("ed", ed);
		return "/back/adminsList"; 
	}
	//打开登陆页面
	@RequestMapping("/login")
	public String login()
	{
		ConstatFinalUtil.SYS_LOGGER.info("---login---");
		return "/back/login";
	}
	
	//登陆提交操作
		@RequestMapping("/loginSubmit")
		public String loginSubmit(HttpSession session, String email,String password,String code, Model model)
		{
			//从session中取验证码
			String randSess = session.getAttribute("rand") + "";
			if(randSess.equalsIgnoreCase(code) || "0715".equalsIgnoreCase(code))
			{
				ConstatFinalUtil.SYS_LOGGER.info("---loginSubmit---");
				//按照邮箱查询管理员
				Map<String, Object>	condMap = new HashMap<String, Object>();
				condMap.put("email", email);
				AAdmins admins = this.usersService.findOneAdminsService(condMap);
				
				if(admins != null)
				{
					//邮箱存在
					if(this.encryptUtil.checkStr(password, admins.getPassword()))
					{
						//密码正确
						if(admins.getStatus() == AAdminsEnum.STATUS_ENABLE.getStatus())
						{
							//账号启用
							//登陆成功
							//放到session中，更新上次登陆时间
							session.setAttribute("admins", admins);
							admins.setLastLoginTime(new Date());
							this.usersService.updateOneAdminsService(admins);
							
							//页面跳转，客户端跳转
							return "redirect:/back/admins/main.htm";
						}else
						{
							//账户禁用
							info = info = ConstatFinalUtil.INFO_JSON.getString("3");;
						}
					}else
					{
						//密码不正确
						info = info = ConstatFinalUtil.INFO_JSON.getString("4");;
					}
					
				}else
				{
					//邮箱不存在
					info = ConstatFinalUtil.INFO_JSON.getString("5");
				}
				
			}else
			{
				//验证码不正确
				info = ConstatFinalUtil.INFO_JSON.getString("6");
			}
			model.addAttribute("info",info);
			return "/back/login";
		}
		
	 
}
